package com.yxjx.duoxue.push;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;

import com.yxjx.duoxue.JumpActivity;
import com.yxjx.duoxue.Yx;
import com.yxjx.duoxue.activities.ActivityIntroductionActivity;
import com.yxjx.duoxue.ad.AdPageActivity;
import com.yxjx.duoxue.course.CourseIntroductionActivity;
import com.yxjx.duoxue.course.SchoolIntroductionActivity;
import com.yxjx.duoxue.course.TeacherIntroductionActivity;
import com.yxjx.duoxue.course.datamodel.AdDTS;
import com.yxjx.duoxue.datamodel.DMAdItem;
import com.yxjx.duoxue.datamodel.DMObject;
import com.yxjx.duoxue.datamodel.DMProduct;
import com.yxjx.duoxue.datamodel.DMTeacher;
import com.yxjx.duoxue.datamodel.school.DMSchool;
import com.yxjx.duoxue.message.MessageListActivity;
import com.yxjx.duoxue.my.MyPageFragment;
import com.yxjx.duoxue.network.NwUtils;
import com.yxjx.duoxue.network.UserLoginResponse;
import com.yxjx.duoxue.payment.CouponListActivity;
import com.yxjx.duoxue.payment.MyOrderList;
import com.yxjx.duoxue.payment.OrderInfoActivity;
import com.yxjx.duoxue.payment.ProductInfoActivity;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

public class PushHelper {
	// XG推送，点击可以打开课程、学校、老师的详情页,广告页，也可以打开课程、学校（针对指定分类）的列表页面
	// 一共有如下五种key，value都为String类型
	public static final String KEY_TYPE = "key_type"; // 推送的页面类型
	public static final String KEY_TYPE_ID = "key_type_id"; //推送页面的ID，例如活动id，老师id等
	public static final String KEY_EXTRA = "key_extra"; // 列表的额外参数 不支持
	// 以下key针对H5页面
	public static final String KEY_URL = "key_url";
	public static final String KEY_TITLE = "key_title";
	public static final String KEY_CONTENT = "key_content";
	public static final String KEY_IS_SHARED = "key_is_shared"; // 0不分享，1分享。
	public static final String KEY_ICON = "key_icon";
	
	// key_type的取值有如下可能
	public static final String TYPE_COURSE = "course";
	public static final String TYPE_SCHOOL = "school";
	public static final String TYPE_TEACHER = "teacher";
	public static final String TYPE_ACTIVITY = "activity";
	public static final String TYPE_AD = "ad";
	// 新增
	public static final String TYPE_PRODUCT = "product";
	public static final String TYPE_ORDER_INFO = "orderinfo";
	public static final String TYPE_PAGE = "page";
	// Activity 页面的取值
	public static final String VALUE_PAGE_MYCOUPON = "mycoupon";
	public static final String VALUE_PAGE_MYORDERS = "myorders";
	
	// public static final String TYPE_COURSE_LIST = "course_list";// 不支持
	// public static final String TYPE_SCHOOL_LIST = "school_list";// 不支持
	// key_type_id的id取值为数字组成的value
	// key_extra的取值是一个json对象的字符串形式

	public static void pushActivity(Context context, String custom) {
		if (UIHelper.isEmpty(custom)) {
			return;
		}
		
		try {
			JSONObject jo = new JSONObject(custom);
			
			String type = DMObject.castString(jo, KEY_TYPE);
			String typeId = DMObject.castString(jo, KEY_TYPE_ID);
			String url = DMObject.castString(jo, KEY_URL);
			String title = DMObject.castString(jo, KEY_TITLE);
			String content = DMObject.castString(jo, KEY_CONTENT);
			Integer isShared = DMObject.getInteger(jo, KEY_IS_SHARED);
			String iconUrl = DMObject.getString(jo, KEY_ICON);
			
			if (TYPE_COURSE.equalsIgnoreCase(type)) {
				Intent intent = new Intent();;
				intent.putExtra(NwUtils.KEY_ID, typeId);
				intent.setClass(context, CourseIntroductionActivity.class);
				context.startActivity(intent);
			} else if (TYPE_SCHOOL.equalsIgnoreCase(type)) {
				Intent intent = new Intent();
				intent.putExtra(NwUtils.KEY_ID, "" + typeId);
				intent.setClass(context, SchoolIntroductionActivity.class);
				context.startActivity(intent);
			} else if (TYPE_TEACHER.equalsIgnoreCase(type)) {
				Intent intent = new Intent(context, TeacherIntroductionActivity.class);
				intent.putExtra(NwUtils.KEY_ID, "" + typeId);
				context.startActivity(intent);
			} else if (TYPE_AD.equalsIgnoreCase(type)) {
				Intent intent = new Intent(context, AdPageActivity.class);
				intent.putExtra(Yx.KEY_TITLE, title);
				intent.putExtra(Yx.KEY_URL, url + UserLoginResponse.getPhoneNum(context));
				// DMAd object.
				DMAdItem adItem = new DMAdItem(null, 0, 0);
				adItem.setTitle(title);
				adItem.setImageUrl(iconUrl);
				adItem.setDes(content);
				adItem.setPageUrl(url + UserLoginResponse.getPhoneNum(context));
				adItem.setIsShared(isShared);
				intent.putExtra(Yx.EXTRA_AD_OBJECT, adItem);
				context.startActivity(intent);
			} else if (TYPE_ACTIVITY.equalsIgnoreCase(type)) {
				Intent intent = new Intent(context, ActivityIntroductionActivity.class);
				intent.putExtra(Yx.EXTRA_DM_ACTIVITY_ID, "" + typeId);
				context.startActivity(intent);
			} else if (TYPE_PRODUCT.equalsIgnoreCase(type)) {
				// 商品信息
				Intent intent = new Intent(context, ProductInfoActivity.class);
				DMProduct extraProduct = new DMProduct();
				extraProduct.setId(Long.valueOf(typeId));
				intent.putExtra(Yx.KEY_PRODUCT_OBJECT, extraProduct);
				context.startActivity(intent);
			} else if (TYPE_ORDER_INFO.equalsIgnoreCase(type)) {
				// 订单信息
				Intent intent = new Intent(context, OrderInfoActivity.class);
				intent.putExtra(Yx.KEY_ORDER_ID, typeId);
				context.startActivity(intent);
				
			} else if (TYPE_PAGE.equalsIgnoreCase(type)) {
				if (VALUE_PAGE_MYCOUPON.equalsIgnoreCase(typeId)) {
					// 我的优惠劵
					MyPageFragment.startMyCoupon(context);
				} else if (VALUE_PAGE_MYORDERS.equalsIgnoreCase(typeId)) {
					// 我的订单
					context.startActivity(new Intent(context, MyOrderList.class));
				}
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
	}
	
	private static String get(JSONObject jo, String key) {
		
		try {
			String ret = (String) jo.get(key);
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * cast AdDTS to String for pushActivity.
	 * @param adItem
	 * @return
	 */
	public static String AdDTS2Custom(AdDTS adItem) {
		if (adItem == null || UIHelper.isEmpty(adItem.getAdUrl())) {
			return "";
		}
		
		String url = adItem.getAdUrl();
		
		if (url.startsWith(JumpActivity.PREFIX_DATA)) {
			//课程，学校，老师，活动，套餐，订单，我的代金劵，我的订单
			JSONObject jo = new JSONObject();
			String[] parseResult = Utils.parseSchema(url);
			if (parseResult == null) {
				return "";
			}
			try {
				jo.put(PushHelper.KEY_TYPE, parseResult[0]);
				jo.put(PushHelper.KEY_TYPE_ID, parseResult[1]);
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return jo.toString();
			
		} else if (url.startsWith("http")) {
			// 专题
			JSONObject jo = new JSONObject();
			try {
				jo.put(PushHelper.KEY_TYPE, PushHelper.TYPE_AD);
				jo.put(PushHelper.KEY_ICON, adItem.getAdImage());
				jo.put(PushHelper.KEY_URL, adItem.getAdUrl());
				jo.put(PushHelper.KEY_TITLE, adItem.getAdTitle());
				jo.put(PushHelper.KEY_CONTENT, adItem.getAdDes());
				jo.put(PushHelper.KEY_IS_SHARED, adItem.getIsShare());
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return jo.toString();
		}
		return "";
	}

}
